using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using System.Text;

namespace PDO.DataPersistence.ServiceProvider

{
    [DataContract]
    public sealed class DataRequest
    {

        public enum DatabaseTarget
        {
            Automatic = 0,
            Local = 1,
            Central = 2,
            Audit = 4
        }


        private string _sqlCommandString;
        private List<object> _parameters;
        private CommandType _commandType;
        private DatabaseTarget _target;
		private string _transactionID;

        private DataRequest()
        {
        }

        public DataRequest(DatabaseTarget target)
            : this()
        {
            _target = target;
            _commandType = CommandType.Text;
            if (null != System.Transactions.Transaction.Current)
            {
                _transactionID = System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier;    
            }            
        }

        [DataMember]
        public NotificationMessage NotificationMessage { get; set; }

        [DataMember]
        public DatabaseTarget Target
        {
            get { return _target; }
            set { _target = value; }
        }

        [DataMember]
        public CommandType CommandType
        {
            get { return _commandType; }
            set { _commandType = value; }
        }

        /// <summary>
        /// The command to be executed on a remote host.
        /// </summary>
        [DataMember]
        public string SqlCommandString
        {
            get { return _sqlCommandString; }
            set { _sqlCommandString = value; }
        }

        /// <summary>
        /// Parameters to stored procedure.
        /// </summary>
        /// <remarks>
        /// These objects must be of SQLDB.type </remarks>
        [DataMember]
        public List<object> Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        [DataMember]
        public bool EtherealRequest { get; set; }

        [DataMember]
        public bool IsAudit { get; set; }

        [DataMember]
        public OperationType SqlOperationType { get; set; }

		[DataMember]
		public string TransactionID
		{
			get { return _transactionID; }
			set { _transactionID = value; }
		}

		// only used for local write through, no need to be a data member
		public ExecuteType ExecuteType
		{
			get; set;
		}

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendFormat("spName: {0}", _sqlCommandString);
            sb.AppendLine();

            if (null != _parameters)
            {
                sb.Append("Parameters: ");
                _parameters.ForEach(new Action<object>((object obj) =>
                {
                    if (obj != null)
                    {
                        sb.AppendFormat("{0} ", obj.ToString());
                    }
                    else
                    {
                        sb.Append("null ");
                    }

                }));

                sb.AppendLine();
            }
            return sb.ToString();
        }
    }

    public enum OperationType
    {
        Undefined,
        Create,
        Read,
        Update,
        Delete,
        Unknown
    }

	public enum ExecuteType
	{
		Unknown = 0,
		Dataset,
		NonQuery,
		Scalar,
		BeginTran,
		CommitTran,
		RollbackTran
	}


}
